package com.yanqu.road.entity.grandchild;

import com.yanqu.road.entity.DataObject;
import com.yanqu.road.entity.FieldTypeAnnotation;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@FieldTypeAnnotation(tableName = "t_c_grand_child_server_exam")
public class ServerExamData extends DataObject {

    @FieldTypeAnnotation(desc = "服务器ID", select = true, update = true)
    private long serverId;

    @FieldTypeAnnotation(desc = "当前科举届数")
    private long examTimes;

    @FieldTypeAnnotation(desc = "最近几场科举ID")
    private List<Long> globalList = new ArrayList<>();

    @FieldTypeAnnotation(desc = "所有科举届数")
    private long allExamTimes;

    @FieldTypeAnnotation(desc = "所有科举记录")
    private List<Long> allGlobalList = new ArrayList<>();

    public void addExamRecord(long globalId, int maxCount) {
        synchronized (this) {

            // 移除第一个
            while (globalList.size() >= maxCount) {
                globalList.remove(0);
            }
            while (allGlobalList.size() >= maxCount) {
                allGlobalList.remove(0);
            }

            examTimes++;
            allExamTimes++;
            globalList.add(globalId);
            allGlobalList.add(globalId);

            setUpdateOption();
        }
    }

    public void removeEmptyExam(long globalId) {
        synchronized (this) {
            boolean remove = globalList.remove(globalId);
            if (remove) {
                examTimes--;
            }
            setUpdateOption();
        }
    }

    public long getSession(long globalId) {
        synchronized (this) {
            // 倒叙遍历找第几届
            for (int i = globalList.size() - 1; i >= 0; i--) {

                long session = examTimes - (globalList.size() - 1 - i);
                long id = globalList.get(i);

                if (id == globalId) {
                    return session;
                } else if (id < globalId) {
                    break;
                }
            }
        }
        return 0;
    }

    // ======================================================

    public long getServerId() {
        return serverId;
    }

    public void setServerId(long serverId) {
        if (!Objects.equals(this.serverId, serverId)) {
            this.serverId = serverId;
            setUpdateOption();
        }
    }

    public long getExamTimes() {
        return examTimes;
    }

    public void setExamTimes(long examTimes) {
        if (!Objects.equals(this.examTimes, examTimes)) {
            this.examTimes = examTimes;
            setUpdateOption();
        }
    }

    public List<Long> getGlobalList() {
        return globalList;
    }

    public void setGlobalList(List<Long> globalList) {
        if (globalList == null) {
            return;
        }
        this.globalList = globalList;
        setUpdateOption();
    }

    public long getAllExamTimes() {
        return allExamTimes;
    }

    public void setAllExamTimes(long allExamTimes) {
        if (this.allExamTimes != allExamTimes) {
            this.allExamTimes = allExamTimes;
            setUpdateOption();
        }
    }

    public List<Long> getAllGlobalList() {
        return allGlobalList;
    }

    public void setAllGlobalList(List<Long> allGlobalList) {
        if (allGlobalList != null) {
            this.allGlobalList = allGlobalList;
            setUpdateOption();
        }
    }
}
